//
//  HMRealName.m
//  SNS_Login-register
//
//  Created by hy on 2021/10/29.
//  Copyright © 2021 lzxuan. All rights reserved.
//

#import "HWBindPhoneView.h"
#import "HWGameMgr.h"
#import "HWConstant.h"
#import "HWButton.h"

@interface HWBindPhoneView()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *tips;

@property (weak, nonatomic) IBOutlet UITextField *userAccountTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet HWButton *sendBtn;
@end

@implementation HWBindPhoneView


+ (instancetype)HWBindPhoneView{
    NSString  *clsName = NSStringFromClass([self class]);
    UINib * nib = [UINib nibWithNibName:clsName bundle:nil];
    HWBindPhoneView * obj = [nib instantiateWithOwner:nil options:nil].firstObject;
    [obj initGameView];
    return obj;
    
}


-(void)updateData:(NSString*)str{
    self.userAccountTextField.text = str;
}

-(void)initGameView{
    // 创建一个富文本对象
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    // 设置富文本对象的颜色
    attributes[NSForegroundColorAttributeName] = [UIColor grayColor];
    _userAccountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:attributes];
    _phoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:attributes];
    _idTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入验证码" attributes:attributes];
    
    //输入框处理
    [_userAccountTextField addTarget:self action:@selector(LimitTextFieldLength2:) forControlEvents:UIControlEventEditingChanged];
    [_phoneTextField addTarget:self action:@selector(LimitTextFieldLength:) forControlEvents:UIControlEventEditingChanged];
    [_idTextField addTarget:self action:@selector(LimitTextFieldLength1:) forControlEvents:UIControlEventEditingChanged];
    _phoneTextField.delegate = self;
//    _userAccountTextField.delegate = self;
    _idTextField.delegate = self;
}

- (void)startWithTimerCount:(NSInteger)count{
    [_sendBtn startWithTimerCount:count];
}
- (void)stopTimer
{
    [_sendBtn stopTimer];
}

#pragma mark - 提交
- (IBAction)commitBtnClick:(id)sender {
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
    [self.idTextField resignFirstResponder];
    if (kUseJs) {
        NSDictionary* dic = [[NSDictionary alloc] initWithObjects:@[self.userAccountTextField.text,self.phoneTextField.text, _idTextField.text ] forKeys:@[@"username", @"phoneNumber" , @"verificationCode"]];
        [[HWGameMgr shared] callToJsWithMethed:kSDKBindPhoneCallBack andDic:dic];
    } else {
        if (self.phoneTextField.text == NULL) {
            [[HWGameMgr shared] showToastWithMsg:@"请输入手机号"];
            return;
        }
        if (self.idTextField.text == NULL) {
            [[HWGameMgr shared] showToastWithMsg:@"请输入验证码"];
            return;
        }
        [[HWGameMgr shared] httpBindPhone:@{@"phoneNumber":self.phoneTextField.text,@"verificationCode":self.idTextField.text} ];
    }
}

#pragma mark - 提交
- (IBAction)sendBtnClick:(id)sender {
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
    [self.idTextField resignFirstResponder];
    if (kUseJs) {
        [[HWGameMgr shared] callToJsWithMethed:kSDKClickGetCodeCallBack andDic:@{@"phoneNumber":_phoneTextField.text}];
    } else {
        if (self.phoneTextField.text == NULL) {
            [[HWGameMgr shared] showToastWithMsg:@"请输入手机号"];
            return;
        }
        [_sendBtn startWithTimerCount:30];
        [[HWGameMgr shared] httpGetCode:@{@"phoneNumber":self.phoneTextField.text,@"verificationCode":self.idTextField.text} ];
    }
}


#pragma mark - 关闭
- (IBAction)closeBtnClick:(id)sender {
    //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
    [self.idTextField resignFirstResponder];
    self.hidden = true;
    if (!kUseJs) {
        [[HWGameMgr shared] showBall];
    }
}

- (void)switchToLogin{
//    _title.text = @"登录";
//    _tips.text = @"没有账号？点此";
//    [_switchBtn setTitle:@"注册" forState:UIControlStateNormal];
//    [_switchBtn setTitle:@"注册" forState:UIControlStateSelected];
//    [_regLoginBtn setTitle:@"登录" forState:UIControlStateNormal];
//    [_regLoginBtn setTitle:@"登录" forState:UIControlStateSelected];
}



#pragma mark - 点击事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        //収键盘
    [self.userAccountTextField resignFirstResponder];
    [self.phoneTextField resignFirstResponder];
    [self.idTextField resignFirstResponder];
}


#pragma mark - delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField!=_phoneTextField) {
        return YES;
    }
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
#pragma mark - 限制文字长度
-(void) LimitTextFieldLength:(id)sender{
    if (_phoneTextField.text.length>11) {
        _phoneTextField.text = [_phoneTextField.text substringToIndex:11];
    }
}
#pragma mark - 限制文字长度
-(void) LimitTextFieldLength1:(id)sender{
    if (_idTextField.text.length>kVerificationCodeLength) {
        _idTextField.text = [_idTextField.text substringToIndex:kVerificationCodeLength];
    }
}
#pragma mark - 限制文字长度
-(void) LimitTextFieldLength2:(id)sender{
    if (_userAccountTextField.text.length>kAccountLength) {
        _userAccountTextField.text = [_userAccountTextField.text substringToIndex:kAccountLength];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}

@end
